import 'dart:async';

import 'package:chathouse/pages/expert/message_list_page.dart';
import 'package:chathouse/pages/expert/service_list_page.dart';
import 'package:flutter/material.dart';
import 'package:chathouse/public/Config.dart';

import '../../public/event_bus.dart';
import '../../util/base_utils.dart';
import '../../util/log.dart';
import '../../widgets/base_appbar.dart';
import '../../widgets/buttons_tabbar.dart';
import '../../widgets/form_widgets/form_widgets.dart';

class ServiceDemandPage extends StatefulWidget {
  const ServiceDemandPage({Key? key}) : super(key: key);

  @override
  _ServiceDemandPageState createState() => _ServiceDemandPageState();
}

class _ServiceDemandPageState extends State<ServiceDemandPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {

  // AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin

  @override
  bool get wantKeepAlive => true;
  late TabController tabController;
  StreamSubscription? refreshSubscription;
  int idx = 0;
  var noReadSericeNum = -1;

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 2, vsync: this);
    refreshSubscription =
        eventBus.on<UpdateClientMessageEvent>().listen((event) {
      if (!mounted) return;
      noReadSericeNum = SpUtil.getInt(Keys.seslistReadNum);
      print("noReadSericeNum:${noReadSericeNum}");
      tabController.animateTo(event.idx as int);
      setState(() {

      });
    });
    loadMessageNum();
    tabController.addListener(updateIdx);
  }

  updateIdx() {
    idx = tabController.index;
    setState(() {

    });
  }

  loadMessageNum() {
    final params = {"kind": "1"};
    Apis.readNum(params).then((value) {
      if (value == null || value.data == null) {
        return;
      }
      noReadSericeNum = value.data["data"]["seslistReadNum"];
      setState(() {});
    }).onError((error, stackTrace) {});
  }
  @override
  void dispose() {
    tabController.dispose();
    refreshSubscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // super.build(context);
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: getAppBar(leading: Container(), title: "服務需求信息"),
      body: Column(
        children: [
          Expanded(
              child: DefaultTabController(
            length: 2,
            child: Column(
              children: <Widget>[
                ButtonsTabBar(
                  controller: tabController,
                  duration: 0,
                  labelStyle: getTextStyle(16,
                      color: MyColors.red_2166474, fontType: FontType.BOLD),
                  unselectedLabelStyle: getTextStyle(16,
                      color: MyColors.black_0000011, fontType: FontType.BOLD),
                  contentPadding: EdgeInsets.symmetric(horizontal: 40.w),
                  buttonMargin:
                      EdgeInsets.only(bottom: 0, right: 5.w, top: 12.w),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(14.w),
                          topRight: Radius.circular(14.w))),
                  unselectedDecoration: BoxDecoration(
                      color: MyColors.gary_210210210,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(14.w),
                          topRight: Radius.circular(14.w))),
                  tabs: [
                    const Tab(
                      text: "服務",
                    ),
                    Tab(
                        // text: "對話記錄",
                        child: Row(
                      children: [
                        Container(
                          child: Text(
                            "對話記錄",
                            style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.bold,
                                color: idx == 0
                                    ? MyColors.black_0000011
                                    : Styles.color_F93A4A),
                          ),
                        ),
                        SizedBox(
                          width: noReadSericeNum > 0 ? 4 : 0,
                        ),
                        Container(
                          decoration: BoxDecoration(
                              color: noReadSericeNum > 0? Styles.color_F93A4A : Colors.transparent,
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(4))),
                          width: 8,
                          height: 8,
                        ),
                      ],
                    )),
                  ],
                ).setContainer(
                    alignment: Alignment.center,
                    height: 45.w,
                    width: double.infinity,
                    color: MyColors.gary_232233237),
                Expanded(
                  child: TabBarView(
                    controller: tabController,
                    children: const <Widget>[
                      ServiceListPage(),
                      MessageListPage(),
                    ],
                  ).setContainer(
                      margin: EdgeInsets.only(left: 16, right: 16, top: 6.w)),
                ),
              ],
            ),
          )),
        ],
      ),
    );
  }
}
